/**
 * @file spell.c
 * @brief Macros of the spell functions.
 * @date Dec-2011
 * @author Leonardo Lino 2101101
 * @author Joaquim Ferreira 2101104
 * @version 1.0
 */
 
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>



#include "debug.h"
#include "files.h"
#include "memory.h"
#include "crypt.h"
#include "listas.h"
#include "hashtables.h"
#include "str_utils.h"
#include "spell.h"
#include "toperrors.h"

#define ERR_IO 1

void mainTopErrors(char *foldername, const char *dictionary, int numThreads){
	HASHTABLE_T* dictHolder;
	struct stTopErr *finalResults;
	LISTA_GENERICA_T* fileList;
	
	
	/**< @brief Load dictionary.*/
	dictHolder= dictLoader(dictionary);
	G_proc_files++;
	
	fileList= folderProcessor(dictHolder, foldername);
	
	outputList(fileList);
	
	
}

LISTA_GENERICA_T* folderProcessor(HASHTABLE_T* dictHolder, char *foldername ){
	 LISTA_GENERICA_T *foldersList = NULL;
	 char *currentFolder;
	 char *full_path;
	 LISTA_GENERICA_T *txtList = NULL;
	 DIR *dir = NULL;
	 struct dirent item, *result = NULL;
	 struct stat stats;
	 struct dirent *dp;
	 struct dirent entry;
	 int return_code;
	 
	 foldersList = lista_criar (free);
	 txtList = lista_criar (free);
	 
	 
	 if((dir = opendir(foldername)) == NULL)
			ERROR(ERR_IO,"Opendir error!");
	
	 for (return_code = readdir_r(dir, &entry, &result);
         result != NULL && return_code == 0;
         return_code = readdir_r(dir, &entry, &result)){
      printf("  %s\n", entry.d_name);
	}
    if (return_code != 0)
      perror("readdir_r() error");
    closedir(dir);
	
	
	
	
	 
	/* 
	 
	 lista_inserir_inicio (foldersList, foldername);
	 
	 while (lista_numero_elementos (foldersList) && G_continue) {  
		 currentFolder = lista_remover_inicio (foldersList);
		 
		 if((dir = opendir(currentFolder)) == NULL)
			ERROR(ERR_IO,"Opendir error!");
			
			
			
		if(readdir_r(dir,&item,&result) != 0) 
			ERROR(ERR_IO,"Readdir Error!");
			
		if(result == NULL) break;
        
        if(strcmp(item.d_name,".") == 0 || strcmp(item.d_name,"..") == 0)
			continue;
			
		printf("currentFolder:%s \n", currentFolder);
        
        
        full_path = path_cat(currentFolder,item.d_name);
        
        printf("item.d_name:%s \n", item.d_name);
        
		if(lstat(full_path,&stats) != 0)
			ERROR(ERR_IO,"Stat error on file %s!", full_path);
			
		if(S_ISDIR(stats.st_mode)) {
			lista_inserir_inicio(foldersList, full_path);
		} else if(S_ISREG(stats.st_mode)) {
			if (check_file_type (full_path, ".txt")) {
				lista_inserir(txtList,full_path);
			} else {
            MY_FREE (full_path);
			}
		}
      
	 }
	 
*/

return txtList;	
}
